package com.menggudf.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.menggudf.entity.LinkAricle;

@Repository
public class LinkAricleDao {

    private static final Log log = LogFactory.getLog(LinkAricleDao.class);

    @PersistenceContext(unitName = "medicalUnit")
    private EntityManager entityManager;

    public void persist(LinkAricle transientInstance) {
        try {
            entityManager.persist(transientInstance);
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void remove(LinkAricle persistentInstance) {
        try {
            entityManager.remove(persistentInstance);
        } catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }

    public LinkAricle merge(LinkAricle detachedInstance) {
        try {
            LinkAricle result = entityManager.merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public LinkAricle findById(Integer id) {
        try {
            LinkAricle instance = entityManager.find(LinkAricle.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<LinkAricle> getLinkAricleList(
            String title,
            String source,
            Date startTime,
            Date endTime,
            int pageSize,
            int offset) {
        StringBuffer sqld = new StringBuffer("from LinkAricle l where l.startTime>=?1 and l.startTime<=?2 ");
        int sum = 2;
        if(!StringUtils.isEmpty(title)){
            sqld.append(" and l.title like:title");
        }
        if(!StringUtils.isEmpty(source)){
            sqld.append(" and l.source=?"+(++sum));
        }
        sqld.append(" order by l.startTime desc ");
        Query query = entityManager.createQuery(sqld.toString()).setParameter(1, startTime).setParameter(2, endTime);
        sum = 2;
        if(!StringUtils.isEmpty(title)){
            query.setParameter("title", "%"+title+"%");
        }
        if(!StringUtils.isEmpty(source)){
            query.setParameter(++sum, source);
        }
        query.setFirstResult(offset).setMaxResults(pageSize);
        return query.getResultList();
    }
    
    public long getLinkAricleListCount(
            String title,
            String source,
            Date startTime,
            Date endTime) {
        StringBuffer sqld = new StringBuffer("select count(id) from LinkAricle l where l.startTime>=?1 and l.startTime<=?2 ");
        int sum = 2;
        if(!StringUtils.isEmpty(title)){
            sqld.append(" and l.title like:title");
        }
        if(!StringUtils.isEmpty(source)){
            sqld.append(" and l.source=?"+(++sum));
        }
        Query query = entityManager.createQuery(sqld.toString()).setParameter(1, startTime).setParameter(2, endTime);
        sum = 2;
        if(!StringUtils.isEmpty(title)){
            query.setParameter("title", "%"+title+"%");
        }
        if(!StringUtils.isEmpty(source)){
            query.setParameter(++sum, source);
        }
        return (long) query.getSingleResult();
    }
    
}
